package com.javacode2018.demo2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component
public class UserService implements IUserService {
    @Autowired
    protected UserDao userDao;
    @Autowired
    private JdbcTemplate jdbcTemplate1;
    @Autowired
    private JdbcTemplate jdbcTemplate2;
    @Autowired
    private UserService userService;

    @Transactional(transactionManager = "transactionManager1")
    @Override
    public void insert1() {
        this.userDao.insert(this.jdbcTemplate2, "tom1");
        this.userService.insert2();
        System.out.println("insert1");
    }

    @Transactional(transactionManager = "transactionManager2")
    @Override
    public void insert2() {
        this.userDao.insert(this.jdbcTemplate2, "tom2");
        this.userService.insert3();
        System.out.println("insert2");
    }

    @Transactional(transactionManager = "transactionManager1")
    @Override
    public void insert3() {
        this.userDao.insert(this.jdbcTemplate1, "tom3");
        System.out.println("insert3");
    }
}
